package com.jumi.microservice.service.admin;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jumi.microservice.common.base.dto.ExportExcelCommonResponse;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.constant.DelFlag;
import com.jumi.microservice.common.page.TableDataInfo;
import com.jumi.microservice.common.utils.EnumUtil;
import com.jumi.microservice.constant.reissue.*;
import com.jumi.microservice.dto.reissue.*;
import com.jumi.microservice.entity.Reissue;
import com.jumi.microservice.mapper.ReissueMapper;
import com.jumi.microservice.material.dto.supplier.JmSupplierResponse;
import com.jumi.microservice.material.service.IJmSupplierService;
import com.jumi.microservice.material.service.IJmWarehouseService;
import com.jumi.microservice.service.IMemberService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author Scw
 * @Date 2020-08-08 15:31
 */
@Service
public class AdminReplacementService {
    @Resource
    private ReissueMapper reissueMapper;
    @DubboReference
    private IJmSupplierService supplyClient;
    @DubboReference
    private IJmWarehouseService warehouseClient;

    /**
     * 导出excel
     *
     * @param request
     * @return
     */
    public ExportExcelCommonResponse<ExportReplacementListResponse> exportExcel(ExportReplacementListRequest request) {
        request.checkRequest();
        QueryWrapper<Reissue> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("reissue_type", ReissueTypeEnum.REISSUE_REPLACE.getCode());
        queryWrapper.orderByDesc("id");
        //审核状态
        if (request.getReissueStatus() != null) {
            queryWrapper.eq("status", request.getReissueStatus());
        }
        //换货原因
        if (request.getReissueReason() != null) {
            queryWrapper.eq("reissue_reason", request.getReissueReason());
        }
        //商品名称
        if (request.getGoodsName() != null && !"".equals(request.getGoodsName())) {
            queryWrapper.like("goods_name", request.getGoodsName());
        }
        //供货商类型：1-供货商【非自营】 2-仓库【自营】
        if (request.getSupplierType() != null) {
            if (request.getSupplierId() != null) {
                queryWrapper.eq("supplier_id", request.getSupplierId());
                if (request.getSupplierType() == com.jumi.microservice.constant.reissue.SupplierTypeEnum.SUPPLIER_TYPE_ONE.getCode().byteValue()) {
                    queryWrapper.eq("self_flag", SelfFlagEnum.SELF_FLAG_NO.getCode());
                }
                if (request.getSupplierType() == com.jumi.microservice.constant.reissue.SupplierTypeEnum.SUPPLIER_TYPE_TWO.getCode().byteValue()) {
                    queryWrapper.eq("self_flag", SelfFlagEnum.SELF_FLAG_YES.getCode());
                }
            }
        }
        //手机号类型：1-购买人手机号 2-收件人手机号
        if (request.getPhoneType() != null) {
            if (request.getPhone() != null && !"".equals(request.getPhone())) {
                if (request.getPhoneType() == PhoneTypeEnum.PHONE_TYPE_BUY.getCode().byteValue()) {
                    queryWrapper.eq("buyer_phone", request.getPhone());
                }
                if (request.getPhoneType() == PhoneTypeEnum.PHONE_TYPE_RECEIVE.getCode().byteValue()) {
                    queryWrapper.eq("receiver_phone", request.getPhone());
                }
            }
        }
        //原订单号
        if (request.getOriginalOrderNo() != null && !"".equals(request.getOriginalOrderNo())) {
            queryWrapper.like("original_order_no", request.getOriginalOrderNo());
        }
        //复选框选择ID
        if (request.getIdStr() != null && !"".equals(request.getIdStr())) {
            queryWrapper.in("id", request.getIdStr().split(","));
        }
        List<Reissue> reissues = reissueMapper.selectList(queryWrapper);
        List<ExportReplacementListResponse> result = new ArrayList<>();
        for (Reissue reissue : reissues) {
            ExportReplacementListResponse reissueListResponse = new ExportReplacementListResponse();
            BeanUtils.copyProperties(reissue, reissueListResponse);
            //类型对照
            ReplacementReasonEnum replacementReasonEnum = EnumUtil.getByCode(reissue.getReissueReason().intValue(), ReplacementReasonEnum.class);
            if (replacementReasonEnum != null) {
                reissueListResponse.setReissueReasonStr(replacementReasonEnum.getDesc());
            }
            //状态对照
            ReissueStatusEnum reissueStatusEnum = EnumUtil.getByCode(reissue.getStatus().intValue(), ReissueStatusEnum.class);
            if (reissueStatusEnum != null) {
                reissueListResponse.setStatusStr(reissueStatusEnum.getDesc());
            }
            String supplierName = "";
            if (reissue.getSelfFlag() == SelfFlagEnum.SELF_FLAG_NO.getCode().byteValue()) {
                //根据供应商ID查询供应商名称 【仓储服务提供接口】
                ResponseResult<JmSupplierResponse> responseResult = supplyClient.findById(reissue.getSupplierId().longValue());
                Object object = responseResult.getData();
                if (object != null) {
                    JmSupplierResponse jmSupplierResponse = JSON.parseObject(JSON.toJSONString(object), JmSupplierResponse.class);
                    supplierName = jmSupplierResponse.getSupplierCompanyName();
                }
            } else {
                //根据实发仓ID查询实发仓名称 【仓储服务提供接口】
                supplierName = warehouseClient.findById(reissue.getSupplierId().longValue()).getData() != null ? warehouseClient.findById(reissue.getSupplierId().longValue()).getData().getWarehouseName() : "";
            }
            reissueListResponse.setBuyNum(reissue.getGoodsAmount());
            reissueListResponse.setActualNum(reissue.getRealSendAmount());
            reissueListResponse.setSupplierName(supplierName);
            reissueListResponse.setReissueNum(reissue.getGoodsNum());
            result.add(reissueListResponse);
        }
        ExportExcelCommonResponse<ExportReplacementListResponse> response = new ExportExcelCommonResponse<>();
        response.setData(result);
        response.setFileName(System.currentTimeMillis() + ".xlsx");
        return response;
    }

    /**
     * 换货列表
     *
     * @param request
     * @return
     */
    public TableDataInfo<ReissueListResponse> replacementList(ReissueListRequest request) {
        QueryWrapper<Reissue> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("reissue_type", ReissueTypeEnum.REISSUE_REPLACE.getCode());
        queryWrapper.orderByDesc("id");
        //换货审核状态
        if (request.getReissueStatus() != null) {
            queryWrapper.eq("status", request.getReissueStatus());
        }
        //换货原因
        if (request.getReissueReason() != null) {
            queryWrapper.eq("reissue_reason", request.getReissueReason());
        }
        //商品名称
        if (request.getGoodsName() != null && !"".equals(request.getGoodsName())) {
            queryWrapper.like("goods_name", request.getGoodsName());
        }
        //供货商类型：1-供货商【非自营】 2-仓库【自营】
        if (request.getSupplierType() != null) {
            if (request.getSupplierId() != null) {
                queryWrapper.eq("supplier_id", request.getSupplierId());
                if (request.getSupplierType() == com.jumi.microservice.constant.reissue.SupplierTypeEnum.SUPPLIER_TYPE_ONE.getCode().byteValue()) {
                    queryWrapper.eq("self_flag", SelfFlagEnum.SELF_FLAG_NO.getCode());
                }
                if (request.getSupplierType() == com.jumi.microservice.constant.reissue.SupplierTypeEnum.SUPPLIER_TYPE_TWO.getCode().byteValue()) {
                    queryWrapper.eq("self_flag", SelfFlagEnum.SELF_FLAG_YES.getCode());
                }
            }
        }
        //手机号类型：1-购买人手机号 2-收件人手机号
        if (request.getPhoneType() != null) {
            if (request.getPhone() != null && !"".equals(request.getPhone())) {
                if (request.getPhoneType() == PhoneTypeEnum.PHONE_TYPE_BUY.getCode().byteValue()) {
                    queryWrapper.eq("buyer_phone", request.getPhone());
                }
                if (request.getPhoneType() == PhoneTypeEnum.PHONE_TYPE_RECEIVE.getCode().byteValue()) {
                    queryWrapper.eq("receiver_phone", request.getPhone());
                }
            }
        }
        //原订单号
        if (request.getOriginalOrderNo() != null && !"".equals(request.getOriginalOrderNo())) {
            queryWrapper.like("original_order_no", request.getOriginalOrderNo());
        }
        IPage<Reissue> page = new Page<>(request.getPageNum(), request.getPageSize());
        page = reissueMapper.selectPage(page, queryWrapper);
        List<Reissue> reissues = page.getRecords();
        List<ReissueListResponse> result = new ArrayList<>();
        if (reissues.size() > 0) {
            for (Reissue reissue : reissues) {
                ReissueListResponse reissueListResponse = new ReissueListResponse();
                BeanUtils.copyProperties(reissue, reissueListResponse);
                //换货类型对照
                ReplacementReasonEnum replacementReasonEnum = EnumUtil.getByCode(reissue.getReissueReason().intValue(), ReplacementReasonEnum.class);
                if (replacementReasonEnum != null) {
                    reissueListResponse.setReissueReasonStr(replacementReasonEnum.getDesc());
                }
                String supplierName = "";
                if (reissue.getSelfFlag() == SelfFlagEnum.SELF_FLAG_NO.getCode().byteValue()) {
                    //根据供应商ID查询供应商名称 【仓储服务提供接口】
                    ResponseResult<JmSupplierResponse> responseResult = supplyClient.findById(reissue.getSupplierId().longValue());
                    Object object = responseResult.getData();
                    if (object != null) {
                        JmSupplierResponse jmSupplierResponse = JSON.parseObject(JSON.toJSONString(object), JmSupplierResponse.class);
                        supplierName = jmSupplierResponse.getSupplierCompanyName();
                    }
                } else {
                    //根据实发仓ID查询实发仓名称 【仓储服务提供接口】
                    supplierName = warehouseClient.findById(reissue.getSupplierId().longValue()).getData() != null ? warehouseClient.findById(reissue.getSupplierId().longValue()).getData().getWarehouseName() : "";
                }
                reissueListResponse.setBuyNum(reissue.getGoodsAmount());
                reissueListResponse.setActualNum(reissue.getRealSendAmount());
                reissueListResponse.setSupplierName(supplierName);
                reissueListResponse.setReissueNum(reissue.getGoodsNum());
                result.add(reissueListResponse);
            }
        }
        TableDataInfo<ReissueListResponse> dataInfo = new TableDataInfo<>();
        dataInfo.setRows(result);
        dataInfo.setTotal(page.getTotal());
        return dataInfo;
    }

    /**
     * 换货原因--对照字典
     */
    public Map<Integer, String> getReplacementReason() {
        Map<Integer, String> m = new HashMap<>();
        m.put(ReplacementReasonEnum.REPLACEMENT_MAKE_MISTAKES.getCode(), ReplacementReasonEnum.REPLACEMENT_MAKE_MISTAKES.getDesc());
        m.put(ReplacementReasonEnum.REPLACEMENT_SHIPMENT_REASON.getCode(), ReplacementReasonEnum.REPLACEMENT_SHIPMENT_REASON.getDesc());
        m.put(ReplacementReasonEnum.REPLACEMENT_EXPRESS_REASON.getCode(), ReplacementReasonEnum.REPLACEMENT_EXPRESS_REASON.getDesc());
        m.put(ReplacementReasonEnum.REPLACEMENT_PRODUCT_REASON.getCode(), ReplacementReasonEnum.REPLACEMENT_PRODUCT_REASON.getDesc());
        m.put(ReplacementReasonEnum.REPLACEMENT_CUSTOMER_REASON.getCode(), ReplacementReasonEnum.REPLACEMENT_CUSTOMER_REASON.getDesc());
        return m;
    }
}
